﻿// <copyright file="GetK3CloudApi.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>

namespace MesAPI.K3Function
{
    using System.Text;
    using System.Text.RegularExpressions;
    using Kingdee.CDP.WebApi.SDK;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Linq;

    /// <summary>
    /// GetK3CloudApi.
    /// </summary>
    public class GetK3CloudApi
    {
        /// <summary>
        /// 获取产品分类.
        /// </summary>
        /// <param name="Code">物料代码.</param>
        /// <param name="msg">返回信息.</param>
        /// <returns>.</returns>
        public static List<object> GetBD_MATERIAL(string Code, out string msg)
        {
            // 用于记录结果
            msg = string.Empty;
            StringBuilder info = new();
            List<object> list = new();
            try
            {
                // 读取配置，初始化SDK
                K3CloudApi client = new();

                // 业务对象标识
                string formId = "BD_MATERIAL";

                // 查询字段集合，即返回哪些数据，不能为空，根据不同业务单据填写不同的字段名
                string fieldKeys = "FSpecification,F_XINJ_Assistant1.FNumber,FWorkShopId.FNAME,FNumber";

                // 请求参数，示例使用的是SDK提供的模板类，还可以使用字符串拼接等方式
                var param = new QueryParam()
                {
                    FormId = formId,
                    FieldKeys = fieldKeys,
                    FilterString = "FNumber = '" + Code + "'",
                };
                var result = client.ExecuteBillQuery(param.ToJson());
                if (result.Count == 1)
                {
                    // 对返回结果进行解析和校验，这里使用的是JsonPatch
                    var resultJObject = JArray.Parse(JsonConvert.SerializeObject(result[0]));
                    var queryNode = resultJObject.SelectToken("$..IsSuccess");
                    if (queryNode != null)
                    {
                        var isSuccess = queryNode.Value<Boolean>();
                        info.AppendLine(isSuccess ? "操作成功" : "操作失败");
                        list = result[0];
                    }
                    else
                    {
                        info.AppendLine("操作成功");
                        list = result[0];
                    }
                }
                else
                {
                    info.AppendLine("操作成功");
                }
            }
            catch (Exception ex)
            {
                info.AppendLine(ex.Message);
            }

            msg = Regex.Replace(info.ToString(), @"[\r\n]", string.Empty);
            return list;
        }

        /// <summary>
        /// 获取客户电话.
        /// </summary>
        /// <param name="name">物料代码.</param>
        /// <param name="msg">返回信息.</param>
        /// <returns>.</returns>
        public static List<object> GetBD_DIANHUA(string name, out string msg)
        {
            // 用于记录结果
            msg = string.Empty;
            StringBuilder info = new();
            List<object> list = new();
            try
            {
                // 读取配置，初始化SDK
                K3CloudApi client = new();

                // 业务对象标识
                string formId = "SAL_DELIVERYNOTICE";

                // 查询字段集合，即返回哪些数据，不能为空，根据不同业务单据填写不同的字段名，以下仅为示例
                string fieldKeys = "FCustomerID.FNumber";

                // 请求参数，示例使用的是SDK提供的模板类，还可以使用字符串拼接等方式
                var param = new QueryParam()
                {
                    FormId = formId,
                    FieldKeys = fieldKeys,
                    FilterString = "FCustomerID.FNumber = '" + name + "'",
                };

                // 调用接口
                var result = client.ExecuteBillQuery(param.ToJson());

                // 返回的数据类型为List<List<object>>，错误的返回结果只有一行数据且IsSuccess为False
                if (result.Count == 1)
                {
                    // 对返回结果进行解析和校验，这里使用的是JsonPatch
                    var resultJObject = JArray.Parse(JsonConvert.SerializeObject(result[0]));
                    var queryNode = resultJObject.SelectToken("$..IsSuccess");
                    if (queryNode != null)
                    {
                        var isSuccess = queryNode.Value<Boolean>();
                        info.AppendLine(isSuccess ? "操作成功" : "操作失败");
                    }
                    else
                    {
                        info.AppendLine("操作成功");
                    }
                }
                else
                {
                    info.AppendLine("操作成功");
                }
            }
            catch (Exception ex)
            {
                info.AppendLine(ex.Message);
            }

            msg = Regex.Replace(info.ToString(), @"[\r\n]", string.Empty);
            return list;
        }
    }
}
